package com.yt.lxh.product.controller;

import com.github.pagehelper.PageInfo;
import com.yt.lxh.common.result.ResponseResult;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.vo.product.CategoryVO;
import com.yt.lxh.model.product.Category;
import com.yt.lxh.dto.product.ListCategoryDTO;
import com.yt.lxh.dto.product.SaveCategoryDTO;
import com.yt.lxh.dto.product.UpdateCategoryDTO;
import com.yt.lxh.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "产品类别管理接口")
@RestController
@RequestMapping("/admin/product/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("添加类别")
    @PostMapping("/add")
    public ResponseResult saveCategory(
            @Validated SaveCategoryDTO saveCategoryDTO,
            @RequestHeader(value = "token", required = false) String token) {
        categoryService.addCategory(saveCategoryDTO, token);
        return ResponseResult.ok(ResultCodeEnum.INSERT_SUCCESS);
    }

    @ApiOperation("获取类别数据（分页）")
    @PostMapping("/page")
    public ResponseResult getCategoryPage(@RequestBody @Validated ListCategoryDTO listCategoryDTO) {
        PageInfo<Category> page = categoryService.getCategoryPage(listCategoryDTO);
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, page);
    }

    @ApiOperation("获取全部类别数据（启用状态）")
    @GetMapping("/list/enable")
    public ResponseResult getCategoryListByEnable() {
        List<CategoryVO> categories = categoryService.getCategoryListByEnableIsTrue();
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, categories);
    }

    @ApiOperation("修改类别")
    @PutMapping("/update")
    public ResponseResult updateCategory(
            @Validated UpdateCategoryDTO updateCategoryDTO,
            @RequestHeader(value = "token", required = false) String token) {
        categoryService.updateCategory(updateCategoryDTO, token);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("逻辑删除类别")
    @DeleteMapping("/delete/{id}")
    public ResponseResult removeCategoryById(
            @PathVariable String id,
            @RequestHeader(value = "token", required = false) String token) {
        categoryService.removeCategoryById(id, token);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }

    @ApiOperation(value = "根据id列表逻辑删除类别")
    @DeleteMapping("/delete/batch")
    public ResponseResult batchRemove(
            @RequestBody List<Long> idList,
            @RequestHeader(value = "token", required = false) String token) {
        categoryService.removeByIds(idList);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }

    @ApiOperation("上传成功标记")
    @PostMapping("/uploadMark")
    public ResponseResult uploadMark() {
        return ResponseResult.ok(ResultCodeEnum.SUCCESS);
    }

}
